package gui.newTournamentWindow;

import gui.helper.CorrectnessTester;
import gui.mainWindow.MainWindow;
import gui.startUpWindow.StartupWindow;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.xml.sax.SAXException;

import tournamentData.GeneralTournamentInformation;
import tournamentData.TournamentData;
import core.Date;
import core.XMLWriter;
import dataBase.DbHolder;

public class NewTournamentWindowController implements Observer {
	GeneralTournamentInformation generalInformation;

	public NewTournamentWindowController() {
		DbHolder.getInstance().getArcherData().addObserver(this);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		try {
			this.setTournamentTypesDropDown();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void setTournamentTypesDropDown() throws SQLException {
		ComboBoxModel x = new DefaultComboBoxModel(DbHolder.getInstance().getArcherData().getAllTournamentTypes());
		NewTournamentWindow.getInstance().getTournamentTypeDropDown().setModel(x);
	}

	protected void setCurrentDate() {
		NewTournamentWindow.getInstance().getStartDateTextBox().setText(Date.getCurrentDate().toString());
		NewTournamentWindow.getInstance().getEndDateTextBox().setText(Date.getCurrentDate().toString());
	}

	private boolean formHasErrors() {
		boolean error = false;

		/** tournament type error handling */
		try {
			error = CorrectnessTester.testCorrectness(error, NewTournamentWindow.getInstance().getTournamentTypeDropDown().getSelectedItem().toString().isEmpty(), NewTournamentWindow.getInstance()
					.getTournamentTypeLabel());
		} catch (NullPointerException e) {
			error = CorrectnessTester.testCorrectness(error, true, NewTournamentWindow.getInstance().getTournamentTypeLabel());
		}

		/** tournament name error handling */
		error = CorrectnessTester.testCorrectness(error, NewTournamentWindow.getInstance().getTournamentNameTextBox().getText().isEmpty(), NewTournamentWindow.getInstance().getTournamentNameLabel());

		/** start-date error handling */
		NewTournamentWindow.getInstance().getStartDateTextBox().setText(new Date(NewTournamentWindow.getInstance().getStartDateTextBox().getText()).toString());
		error = CorrectnessTester.testCorrectness(error, new Date(NewTournamentWindow.getInstance().getStartDateTextBox().getText()).equals(new Date("some error generating format")),
				NewTournamentWindow.getInstance().getStartDateLabel());

		/** end-date error handling */
		NewTournamentWindow.getInstance().getEndDateTextBox().setText(new Date(NewTournamentWindow.getInstance().getEndDateTextBox().getText()).toString());
		error = CorrectnessTester.testCorrectness(error, new Date(NewTournamentWindow.getInstance().getEndDateTextBox().getText()).equals(new Date("some error generating format")),
				NewTournamentWindow.getInstance().getEndDateLabel());

		/** organizer error handling */
		error = CorrectnessTester.testCorrectness(error, NewTournamentWindow.getInstance().getOrganizerTextBox().getText().isEmpty(), NewTournamentWindow.getInstance().getOrganizerLabel());

		/** tournament lead error handling */
		error = CorrectnessTester.testCorrectness(error, NewTournamentWindow.getInstance().getTournamentLeadTextBox().getText().isEmpty(), NewTournamentWindow.getInstance().getTournamentLeadLabel());

		/** referee error handling */
		error = CorrectnessTester.testCorrectness(error, NewTournamentWindow.getInstance().getRefereeTextBox().getText().isEmpty(), NewTournamentWindow.getInstance().getRefereeLabel());

		return error;
	}

	protected void createNewTournament() {
		if (!this.formHasErrors()) {
			String tournamentType = NewTournamentWindow.getInstance().getTournamentTypeDropDown().getSelectedItem().toString();
			String tournamentName = NewTournamentWindow.getInstance().getTournamentNameTextBox().getText();
			Date startDate = new Date(NewTournamentWindow.getInstance().getStartDateTextBox().getText());
			Date endDate = new Date(NewTournamentWindow.getInstance().getEndDateTextBox().getText());
			String organizer = NewTournamentWindow.getInstance().getOrganizerTextBox().getText();
			String tournamentLead = NewTournamentWindow.getInstance().getTournamentLeadTextBox().getText();
			String referee = NewTournamentWindow.getInstance().getRefereeTextBox().getText();

			this.generalInformation = new GeneralTournamentInformation(tournamentType, tournamentName, startDate, endDate, organizer, tournamentLead, referee);
			TournamentData.getInstance().setGeneralTournamentInformation(this.generalInformation);

			/** save tournament */
			try {
				XMLWriter.writeToFile(new File(XMLWriter.getFileName(TournamentData.getInstance().getGeneralTournamentInformation().getTournamentName(), TournamentData.getInstance()
						.getGeneralTournamentInformation().getStartDate())));
			} catch (SAXException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			/** save tournamentType to DB */
			DbHolder.getInstance().getArcherData().saveTournamentType(NewTournamentWindow.getInstance().getTournamentTypeDropDown().getSelectedItem().toString());

			/** open the main Window */
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					MainWindow inst;
					inst = new MainWindow(new File(XMLWriter.getFileName(NewTournamentWindowController.this.generalInformation.getTournamentName(),
							NewTournamentWindowController.this.generalInformation.getStartDate())));
					inst.setLocationRelativeTo(null);
					inst.setVisible(true);
				}
			});

			NewTournamentWindow.getInstance().dispose();

		} else {
			JOptionPane.showMessageDialog(null, "Eingabefehler");
		}

	}

	protected void performCloseOperation() {
		NewTournamentWindow.getInstance().dispose();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				StartupWindow.getInstance().setLocationRelativeTo(null);
				StartupWindow.getInstance().setVisible(true);
			}
		});
	}
}
